package com.situ.mall.controller;

import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.JSONResult;
import com.situ.mall.util.LayUITableJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("/getProductListPage")
    public String getProductListPage(Model model) {
        System.out.println("ProductController.getProductListPage");
//        List<Product> list = productService.selectAll();
//        model.addAttribute("list", list);
        // return "/WEB-INF/product_list.jsp";
        return "product_list";
    }

    // /product/selectByPage?page=1&limit=10&name=zhnagsan&age=23
    @ResponseBody
    @RequestMapping("/selectByPage")
    public LayUITableJSONResult selectByPage(ProductQuery productQuery) {
        LayUITableJSONResult layUITableJSONResult =  productService.selectByPage(productQuery);
        return layUITableJSONResult;
    }

    @ResponseBody
    @RequestMapping("/deleteById")
    public JSONResult deleteById(Integer id) {
        return productService.deleteById(id);
    }

    @ResponseBody
    @RequestMapping("/deleteAll")
    public JSONResult deleteAll(Integer[] ids) {
        System.out.println("deleteALl: " + Arrays.toString(ids));
        return productService.deleteAll(ids);
    }

    @RequestMapping("/getProductAddPage")
    public String getProductAddPage() {
        return "product_add";
    }

    @ResponseBody
    @RequestMapping("/add")
    public JSONResult add(Product product) {
        return productService.add(product);
    }

    @RequestMapping("/getProductUpdatePage")
    public String getProductUpdatePage(Integer id, Model model) {
        Product product = productService.selectById(id);
        model.addAttribute("product", product);
        return "product_update";
    }

    @ResponseBody
    @RequestMapping("/update")
    public JSONResult update(Product product) {
        return productService.update(product);
    }
}
